package ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.app.ActivityOptionsCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseFragment;
import com.fy.fyzf.base.BasePresenter;
import com.fy.fyzf.bean.AddHousingBean1;
import com.fy.fyzf.bean.BuildingDetailBean;
import com.fy.fyzf.bean.HousesBean;
import com.fy.fyzf.bean.HousesListBean;
import com.fy.fyzf.bean.HousesListBean1;
import com.fy.fyzf.presenter.BuildDetailPresenter;
import com.fy.fyzf.view.BuildDetailCallBack;
import com.google.gson.Gson;

import java.util.List;

import butterknife.BindView;
import ui.activity.BuildingDetailActivity;
import ui.activity.HouseDetailActivity;
import ui.adapter.BuildingDetailAdapter;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link BuildListFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BuildListFragment extends BaseFragment<BuildDetailPresenter> implements BuildDetailCallBack {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    @BindView(R.id.recycleView)
    RecyclerView recycleView;

    // TODO: Rename and change types of parameters
    private Integer buildingId;
    private Integer type;
    private BuildingDetailAdapter buildingDetailAdapter;
    private View foot;
    private TextView textView;
    private TextView tvFoot;
    private List<HousesListBean1> newList;
    private List<HousesListBean1> list;
    public BuildListFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment BuildListFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static BuildListFragment newInstance(Integer param1, Integer param2) {
        BuildListFragment fragment = new BuildListFragment();
        Bundle args = new Bundle();
        args.putInt("buildingId", param1);
        args.putInt("type", param2);
        fragment.setArguments(args);
        return fragment;
    }

    private boolean isLoadMore = false;
    private AddHousingBean1 housesBean = new AddHousingBean1();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            buildingId = getArguments().getInt("buildingId");
            type = getArguments().getInt("type");
        }
    }


    @Override
    protected BuildDetailPresenter createPresenter() {
        return new BuildDetailPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_build_list;
    }

    @Override
    public void initView(View rootView) {
        super.initView(rootView);
        buildingDetailAdapter = new BuildingDetailAdapter();
        recycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recycleView.setAdapter(buildingDetailAdapter);
//        housesBean.setPageSize(5);
        foot = LayoutInflater.from(getActivity()).inflate(R.layout.foot_build, null, false);
        tvFoot = foot.findViewById(R.id.tv_foot);
        buildingDetailAdapter.addFooterView(foot);
        foot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isLoadMore = !isLoadMore;

                if (isLoadMore) { //默认false
                    buildingDetailAdapter.setNewData(list);
                    tvFoot.setText("收起");
                } else {
                    tvFoot.setText("展开所有房源");
                    buildingDetailAdapter.setNewData(newList);
                }
            }
        });

        buildingDetailAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<HousesListBean1> listBeans = adapter.getData();
                Intent intent = new Intent(getActivity(), HouseDetailActivity.class);
                intent.putExtra("housingid", listBeans.get(position).getHousingId());
                intent.putExtra("type", 1);
                ImageView imageView = view.findViewById(R.id.iv_logo);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void loadData() {
//        housesBean.setPageNum(1);
        if (type == -1) {
            housesBean.setDimension(null);
        } else {
            housesBean.setDimension(type);
        }
        if (isLoadMore) {
           tvFoot.setText("收起");
        } else {
            tvFoot.setText("展开所有房源");
        }
        housesBean.setFloorId(buildingId);
        mPresenter.getHouseList(housesBean);
    }

    @Override
    public void getBuildingDetailSuccess(BuildingDetailBean buildingDetailBean) {

    }

    private int count = 0;
    @Override
    public void getHouseListSuccess(List<HousesListBean1> housesListBean) {
        list = housesListBean;
        if (list.size() > 5) {
            newList = list.subList(0, 5);
        }else {
            newList = list;
        }
        if (isLoadMore) { //默认false
            buildingDetailAdapter.setNewData(list);
        } else {
            buildingDetailAdapter.setNewData(newList);
        }
        if (housesListBean.size() == 0) {
//            tvFoot.setClickable(true);
            tvFoot.setText("暂无数据");
        } else {

        }
    }
}